import time
from typing import Union

import uvicorn

from FileTrain import RemoteFileTrainAddRequest
from GetFileMessage import GetFileMessageExecute, GetFileMessageRequest
from FileTrain import doFileTrainExecute
from fastapi import FastAPI
from fastapi.openapi.docs import (
    get_redoc_html,
    get_swagger_ui_html,
    get_swagger_ui_oauth2_redirect_html,
)

from ModelPredict import RemoteFilePredictAddRequest, doFilePredictExecute

app = FastAPI()

app = FastAPI(docs_url=None, redoc_url=None)


@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url=app.openapi_url,
        title=app.title + " - Swagger UI",
        oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
        swagger_js_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js",
        swagger_css_url="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css",
    )


@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
    return get_swagger_ui_oauth2_redirect_html()


@app.get("/redoc", include_in_schema=False)
async def redoc_html():
    return get_redoc_html(
        openapi_url=app.openapi_url,
        title=app.title + " - ReDoc",
        redoc_js_url="https://unpkg.com/redoc@next/bundles/redoc.standalone.js",
    )


@app.post("/file/info")
async def getFileMessage(getFileMessageRequest: GetFileMessageRequest):
    return GetFileMessageExecute().getFileInfo(getFileMessageRequest.url)

@app.post("/file/train")
async def doFileTrain(remoteFileTrainAddRequest: RemoteFileTrainAddRequest):
    return doFileTrainExecute().doFileTrain(remoteFileTrainAddRequest)

@app.post("/file/predict")
async def doFilePredict(remoteFilePredictAddRequest: RemoteFilePredictAddRequest):
    return doFilePredictExecute().doFilePredict(remoteFilePredictAddRequest)


if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8124)
